package cn.ubest.mvvmcore.utils;

import android.text.format.Time;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by maoqi on 2018/6/28.
 */
public class TimeUtils {

    /**
     * 是否在同一天
     *
     * @param time1 时间(毫秒)
     * @param time2 时间(毫秒)
     * @return
     */
    public static boolean isSameDay(long time1, long time2) {
        Time time = new Time();
        time.set(time1);

        int timeYear = time.year;
        int timeMonth = time.month;
        int timeMonthDay = time.monthDay;

        time.set(time2);

        return (timeYear == time.year)
                && (timeMonth == time.month)
                && (timeMonthDay == time.monthDay);
    }

    /**
     * 是否在同一月
     *
     * @param time1 时间(毫秒)
     * @param time2 时间(毫秒)
     * @return
     */
    public static boolean isSameMonth(long time1, long time2) {
        Time time = new Time();
        time.set(time1);

        int timeYear = time.year;
        int timeMonth = time.month;

        time.set(time2);

        return (timeYear == time.year)
                && (timeMonth == time.month);
    }

    /**
     * 是否同一年
     *
     * @param time1 时间(毫秒)
     * @param time2 时间(毫秒)
     * @return
     */
    public static boolean isSameYear(long time1, long time2) {
        Time time = new Time();
        time.set(time1);

        int timeYear = time.year;

        time.set(time2);

        return (timeYear == time.year);
    }

    /**
     * 是否是昨天
     *
     * @param when 时间(毫秒)
     * @return
     */
    private static boolean isYesterday(long when) {
        Time time = new Time();
        time.set(when);

        int thenYear = time.year;
        int thenMonth = time.month;
        int thenMonthDay = time.monthDay;

        time.set(getCurrentTimeMs() - (1000 * 3600 * 24));

        return (thenYear == time.year)
                && (thenMonth == time.month)
                && (thenMonthDay == time.monthDay);
    }

    /**
     * 是否是明天
     *
     * @param when 时间(毫秒)
     * @return
     */
    private static boolean isTomorrow(long when) {
        Time time = new Time();
        time.set(when);

        int thenYear = time.year;
        int thenMonth = time.month;
        int thenMonthDay = time.monthDay;

        time.set(getCurrentTimeMs() + (1000 * 3600 * 24));

        return (thenYear == time.year)
                && (thenMonth == time.month)
                && (thenMonthDay == time.monthDay);
    }


    public static String formatTime(long time) {
        return formatTime(time, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化时间
     *
     * @param time 时间(毫秒)
     * @param rule "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String formatTime(long time, String rule) {
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(rule);
        return format.format(date);
    }

    /**
     * 格式化班级圈时间
     *
     * @param time
     * @return
     */
    public static String formatClassesCircleTime(long time) {
        return TimeUtils.formatTime(time, "yyyy-MM-dd HH:mm");
    }

    /**
     * 获取当前时间(秒)
     *
     * @return
     */
    public static Long getCurrentTimeS() {
        return getCurrentTimeMs() / 1000;
    }

    /**
     * 获取当前时间(毫秒)
     *
     * @return
     */
    public static Long getCurrentTimeMs() {
        return System.currentTimeMillis();
    }

    /**
     * 格式化视频显示时间
     *
     * @param lengthTime 视频时长(秒)
     * @return 根据时长返回 时:分:秒 或者 分:秒
     */
    public static String formatVideoTime(int lengthTime) {
        if (lengthTime >= 60 * 60) {
            return formatTime(lengthTime * 1000, "HH:mm:ss");
        }
        return formatTime(lengthTime * 1000, "mm:ss");
    }

    /**
     * 日期转时间戳
     *
     * @param dateStr
     * @return 秒
     */
    public static long dateToStamp(String dateStr) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = simpleDateFormat.parse(dateStr);
            return date.getTime();
        }catch (Exception e){

        }
        return 0;
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 评论通知时间格式化
     */
    public static String formatListTime(long date) {
        long curTime = getCurrentTimeMs();

        if (isSameDay(date, curTime)) {
            //同一天
            long time = curTime - date;
            if (time < 120 * 1000) {
                //小于120s
                return "刚刚";
            } else if (time < 60 * 60 * 1000) {
                //120s到1小时
                return time / (60 * 1000) + "分钟前";
            } else if (time < 24 * 60 * 60 * 1000) {
                //1小时到24小时
                return time / (60 * 60 * 1000) + "小时前";
            } else {
                return formatTime(date);
            }
        } else {
            //不同天
            return formatTime(date);
        }
    }
}
